package common.code;

import java.io.File;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

abstract class SerialNumber {

    public synchronized String getSerialNumber() {
        return process();
    }
    protected abstract String process();
}


abstract class EveryDaySerialNumber extends SerialNumber {
    
    protected final static SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
    protected DecimalFormat df = null;
    
    public EveryDaySerialNumber(int width) {
        if(width < 1) {
            throw new IllegalArgumentException("Parameter length must be great than 1!");
        }
        char[] chs = new char[width];
        for(int i = 0; i < width; i++) {
            chs[i] = '0';
        }
        df = new DecimalFormat(new String(chs));
    }
    
    protected String process() {
        Date date = new Date();
        String no = getOrUpdateNumber(date, 1);
        return no;
        
    }
    
    protected String format(Date date) {
        return sdf.format(date);
    }
    protected String format(int num) {
        return df.format(num);
    }
    
    /**
     * 获得序列号，同时更新持久化存储中的序列
     * @param current 当前的日期
     * @param start   初始化的序号
     * @return 所获得新的序列号
     */
    protected abstract String getOrUpdateNumber(Date current, int start);
}

 public class FileEveryDaySerialNumber extends EveryDaySerialNumber {
	/**
	 * 序列号前缀
	 */
	private String pf="";
    /**
     * 持久化存储的文件
     */
    private File file = null;
    
    /**
     * 存储的分隔符
     */
    private final static String FIELD_SEPARATOR = ",";   

    /**
     * 获取唯一序列号 格式：前缀+日期(位数)<br>
     * 已修改为：支持前缀参数的序列化 update zdy 2011-10-10 <br>
     * @param prefix
     * @param width
     * @param filename
     */
    public FileEveryDaySerialNumber(String prefix,int width, String filename) {
        super(width);
        file = new File(filename);
        pf = prefix;
    }

    @Override
    protected String getOrUpdateNumber(Date current, int start) {
        String date = format(current);
        int num = start;
        boolean append = true;
        String rn = System.getProperty("line.separator");
        if(file.exists()) {
            List<String> list = FileUtil.readList(file);
            if(list.size()>0){
	            for(int i=0;i<list.size();i++){
	            	String[] tmp = list.get(i).split(FIELD_SEPARATOR);
	            	if(tmp[0].equalsIgnoreCase(pf)){
	            		if(date.equals(tmp[1])){
	            			num = Integer.parseInt(tmp[2]);
	            			list.set(i, tmp[0] + FIELD_SEPARATOR + date + FIELD_SEPARATOR + (num + 1));
	            			append = false;
	            		}
	            	}
	            }
            }
//            String[] data = list.get(0).split(FIELD_SEPARATOR);
//            if(date.equals(data[0])) {
//                num = Integer.parseInt(data[1]);
//            }
            if(append){//追加写入
            	FileUtil.rewrite(file, append, pf + FIELD_SEPARATOR + date + FIELD_SEPARATOR + (num + 1)+rn);
            }else{//重新写入
            	StringBuffer bf = new StringBuffer();
            	for(int i=0;i<list.size();i++){
            		bf.append(list.get(i)+rn);
            	}
            	FileUtil.rewrite(file, append, bf.toString());
            }
        }else{
        	System.err.println("文件不存在!");
        }
        return pf+format(current) + format(num);
    }
    
}